package com.xbb.monitor.service;

import com.xbb.monitor.entity.Disk;
import com.xbb.monitor.entity.echarts.AreaStyle;
import com.xbb.monitor.entity.echarts.LineBean;
import com.xbb.monitor.entity.echarts.Normal;
import com.xbb.monitor.repository.DiskRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class DiskService {
    @Autowired
    private DiskRepository diskRepository;
    @PersistenceContext
    private EntityManager entityManager;

    public List<String> getPath(){
        List<String> paths = diskRepository.findAllPath();
        return paths;
    }

    public Map<String,Object> getDataByPath(Calendar calendar, int interval, SimpleDateFormat sdf, String path){
        String sql = "SELECT id,time, path, used, free, mont, name, rs, ws, rkbs, wkbs FROM (\n" +
                "  SELECT b.*,@rownum \\:= @rownum + 1 AS rownum,\n" +
                "  @ptime2 \\:=b.time as ptime2,\n" +
                "  if(TIMESTAMPDIFF(MINUTE,@ptime,b.time)<?2,@ptime=null,@ptime\\:=@ptime2) AS ptime FROM\n" +
                "(SELECT * FROM t_disk WHERE TIME >= ?1 and mont = ?3  ORDER BY time) b,\n" +
                "(SELECT @rownum \\:= 0,@ptime\\:= NULL ,@rank\\:=0,@ptime2\\:=NULL ) a\n" +
                ") AS c WHERE ptime IS NOT NULL";

        Query query = entityManager.createNativeQuery(sql,Disk.class);

        query.setParameter(1,calendar.getTime());
        query.setParameter(2,interval);
        query.setParameter(3,path);
        List<Disk> disks = query.getResultList();

        Map<String,Object> map = new HashMap<>();
        List<LineBean> lineBeansDisk = new ArrayList<LineBean>();
        List<LineBean> lineBeansKbs = new ArrayList<LineBean>();
        List<LineBean> lineBeansS = new ArrayList<LineBean>();
        AreaStyle areaStyle = new AreaStyle(new Normal());
        //硬盘指标
        LineBean lineUsed = new LineBean("used","line","disk",areaStyle);
        LineBean lineFree = new LineBean("free","line","disk",areaStyle);
        //读写字节数
        LineBean readKbs = new LineBean("read","line");
        LineBean writeKbs = new LineBean("write","line");
        //磁盘请求数
        LineBean rs = new LineBean("rs","line");
        LineBean ws = new LineBean("ws","line");

        //硬盘指标
        List<String[]> datasUsed = new ArrayList<String[]>();
        List<String[]> datasFree = new ArrayList<String[]>();
        //读写字节数
        List<String[]> datasReadKbs = new ArrayList<String[]>();
        List<String[]> datasWriteKbs = new ArrayList<String[]>();
        //磁盘请求数
        List<String[]> datasRss = new ArrayList<String[]>();
        List<String[]> datasWss = new ArrayList<String[]>();

        for(Disk disk : disks){
            String[] dataUsed = {sdf.format(disk.getTime()),String.format("%.1f",Double.parseDouble(disk.getUsed())/1024/1024)};
            String[] dataFree = {sdf.format(disk.getTime()),String.format("%.1f",Double.parseDouble(disk.getFree())/1024/1024)};

            String[] dateReadKbs = {sdf.format(disk.getTime()),disk.getRkbs()+""};
            String[] dateWriteKbs = {sdf.format(disk.getTime()),disk.getWkbs()+""};

            String[] dateRs = {sdf.format(disk.getTime()),disk.getRs()+""};
            String[] dataWs = {sdf.format(disk.getTime()),disk.getWs()+""};

            datasUsed.add(dataUsed);
            datasFree.add(dataFree);
            datasReadKbs.add(dateReadKbs);
            datasWriteKbs.add(dateWriteKbs);
            datasRss.add(dateRs);
            datasWss.add(dataWs);
        }

        lineUsed.setData(datasUsed);
        lineFree.setData(datasFree);

        readKbs.setData(datasReadKbs);
        writeKbs.setData(datasWriteKbs);

        rs.setData(datasRss);
        ws.setData(datasWss);

        lineBeansDisk.add(lineUsed);
        lineBeansDisk.add(lineFree);
        map.put("dataDisk",lineBeansDisk);

        lineBeansKbs.add(readKbs);
        lineBeansKbs.add(writeKbs);
        map.put("kbs",lineBeansKbs);

        lineBeansS.add(rs);
        lineBeansS.add(ws);
        map.put("rws",lineBeansS);

        return map;
    }
}
